<?
/**
 * Process.php
 *
 * The Process class is meant to simplify the task of processing
 * user submitted forms, redirecting the user to the correct
 * pages if errors are found, or if form is successful, either
 * way. Also handles the logout procedure.
 */
include("session.php");

class Process
{
        /* Class constructor */
        function Process()
        {
                global $session;

                if(isset($_POST['sublogin']))
                {
                        $this->procLogin();
                }
                else if(isset($_POST['subjoin']))
                {
                        $this->procRegister();
                }
                else if(isset($_POST['subforgot']))
                {
                        $this->procForgotPass();
                }
                else if(isset($_POST['subChangePass']))
                {
                        $this->procChangePass();
                }
                else if(isset($_POST['subedit']))
                {
                        $this->procEditAccount();
                }
                else if(isset($_POST['subPostNews']))
                {
                        $this->procPostNews();
                }
                else if(isset($_POST['subEditNews']))
                {
                        $this->procEditNews();
                }
                else if(isset($_POST['subEditUserField']))
                {
                        $this->procEditUserField();
                }
                else if(isset($_POST['subEditUserInfo']))
                {
                        $this->procEditUserInfo();
                }
                else if(isset($_POST['subEditContactInfo']))
                {
                        $this->procEditContactInfo();
                }
                else if(isset($_POST['subEditLocation']))
                {
                        $this->procEditLocation();
                }
                else if(isset($_POST['subEditPreferences']))
                {
                        $this->procEditPreferences();
                }
                else if(isset($_POST['subAddComment']))
                {
                        $this->procAddComment();
                }
                else if(isset($_GET['deleteComment']))
                {
                        $this->procDeleteComment();
                }
                else if(isset($_POST['subAddResource']))
                {
                        $this->procAddResource();
                }
                else if(isset($_POST['subEditResource']))
                {
                        $this->procEditResource();
                }
                else if(isset($_GET['deleteResource']))
                {
                        $this->procDeleteResource();
                }
                else if(isset($_POST['subSendMessage']))
                {
                        $this->procSendMessage();
                }
                else if(isset($_GET['deleteMessage']))
                {
                        $this->procDeleteMessage();
                }
                else if(isset($_GET['deleteUser']))
                {
                        $this->procDeleteUser();
                }
                else if(isset($_POST['subAddCategory']))
                {
                        $this->procAddCategory();
                }
                else if(isset($_GET['deleteCategory']))
                {
                        $this->procDeleteCategory();
                }
                else if(isset($_POST['subSearch']))
                {
                        $this->procSearch();
                }
                else if(isset($_POST['subAddProject']))
                {
                        $this->procAddProject();
                }
                else if(isset($_POST['subAddNeededResource']))
                {
                        $this->procAddNeededResource();
                }
                else if(isset($_POST['subEditNeededResource']))
                {
                        $this->procEditNeededResource();
                }
                else if(isset($_GET['deleteNeededResource']))
                {
                        $this->procDeleteNeededResource();
                }
                else if(isset($_POST['subEditProjectField']))
                {
                        $this->procEditProjectField();
                }
                else if(isset($_POST['subEditProjectDetails']))
                {
                        $this->procEditProjectDetails();
                }
                else if(isset($_POST['subEditProjectPreferences']))
                {
                        $this->procEditProjectPreferences();
                }
                else if(isset($_GET['deleteProject']))
                {
                        $this->procDeleteProject();
                }
                else if(isset($_POST['subApplication']))
                {
                        $this->procApplication();
                }
                else if(isset($_GET['joinProject']))
                {
                        $this->procJoinProject();
                }
                else if(isset($_GET['leaveProject']))
                {
                        $this->procLeaveProject();
                }
                else if(isset($_POST['subUploadProjectPic']))
                {
                        $this->procUploadProjectPic();
                }
                else if(isset($_POST['subUploadProfilePic']))
                {
                        $this->procUploadProfilePic();
                }
                else if(isset($_POST['subCountryReset']))
                {
                        $this->procCountryReset();
                }
                else if(isset($_POST['subMapAddressReset']))
                {
                        $this->procMapAddressReset();
                }
                else if(isset($_POST['subEditUserlevel']))
                {
                        $this->procEditUserlevel();
                }
                else if(isset($_POST['subAddFeedback']))// User/Guest send feedback
                {
                        $this->procAddFeedback();
                }
                else if(isset($_GET['reviewedFeedback']))
                {
                        $this->procReviewedFeedback();
                }
                else if(isset($_GET['deleteFeedback']))
                {
                        $this->procDeleteFeedback();
                }
                else if(isset($_POST['subAddLink']))// User adds link
                {
                        $this->procAddLink();
                }
                else if(isset($_POST['subEditLink']))
                {
                        $this->procEditLink();
                }
                else if(isset($_GET['deleteLink']))
                {
                        $this->procDeleteLink();
                }
                else if(isset($_POST['subPostProjectNews']))// User posts project news
                {
                        $this->procPostProjectNews();
                }
                else if(isset($_POST['subEditProjectNews']))
                {
                        $this->procEditProjectNews();
                }
                else if(isset($_GET['deleteProjectNews']))
                {
                        $this->procDeleteProjectNews();
                }
                else if(isset($_POST['subEditMemberlevel']))// Project Admin edits members
                {
                        $this->procEditMemberlevel();
                }
                else if(isset($_GET['deleteMember']))
                {
                        $this->procDeleteProjectMember();
                }
                else if(isset($_GET['addLike']))
                {
                        $this->procAddLike();
                }
                else if($session->logged_in)
                {
                        $this->procLogout();
                }
                else
                {
                        header("Location: http://zeitgeistresources.com/");
                }
        }
        /**
        * procAddLike() - Adds like to attached id
        */
        function procAddLike()
        {
                global $session, $database;
               
                $_SESSION['addlikesuccessful'] = $database->addLike($_GET['addLike'],$_GET['type']);
                if($_GET['type']==0)
                {
                        header("Location: ".$session->referrer."?id=".$_GET['addLike']);
                }
                else
                {
                        header("Location: ".$session->referrer);
                }
        }
       
        /**
        * procPostProjectNews() - Posts news
        */
        function procPostProjectNews()
        {
                global $session, $form;
               
                $retval = $session->postProjectNews($_POST['subPostProjectNews'],$_POST['title'],$_POST['content']);
               
                if($retval)
                {
                        $_SESSION['projectnewssuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                }
                header("Location: ".$session->referrer."?id=".$_POST['subPostProjectNews']);
        }
        /**
        * procEditProjectNews() - Posts news
        */
        function procEditProjectNews()
        {
                global $session, $form;
               
                $retval = $session->editProjectNews($_POST['subEditProjectNews'],$_POST['title'],$_POST['content']);
               
                if($retval)
                {
                        $_SESSION['editprojectnewssuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                }
                header("Location: ".$session->referrer."?id=".$_POST['projectID']."&edit=".$_POST['subEditProjectNews']);
        }
        /**
        * procDeleteProjectNews() - Deletes news, if user has capability
        */
        function procDeleteProjectNews()
        {
                global $session, $form;
               
                $retval = $session->deleteProjectNews($_GET['deleteProjectNews'],$_GET['news'],$session->username);
               
                if($retval)
                {
                        $_SESSION['deletenewssuccess'] = true;
                }
                header("Location: ".$session->referrer."?id=".$_GET['deleteProjectNews']);
        }
       
        /**
         * procAddLink - Attempts to add the link to the database.
         */
        function procAddLink()
        {
                global $session, $form;
                $retval = $session->addLink($_POST['creator'], $_POST['link'], $_POST['url'], $_POST['description']);

                if($retval == 0)// Link Successful
                {
                        $_SESSION['linksuccess'] = true;
                        header("Location: ".$session->referrer);
                }
                else if($retval == 1)// Error found with form
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
                else if($retval == 2)// Link attempt failed
                {
                        $_SESSION['linksuccess'] = false;
                        header("Location: ".$session->referrer);
                }
        }
        /**
         * procEditLink - Attempts to add the link to the database.
         */
        function procEditLink()
        {
                global $session, $form;
                $retval = $session->editLink($_POST['subEditLink'], $_POST['link'], $_POST['url'], $_POST['description'], $_POST['creator']);

                if($retval == 0)// Link Successful
                {
                        $_SESSION['linkeditsuccess'] = true;
                        header("Location: ".$session->referrer."?edit=".$_POST['subEditLink']);
                }
                else if($retval == 1)// Error found with form
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
                else if($retval == 2)// Link attempt failed
                {
                        $_SESSION['linkeditsuccess'] = false;
                        header("Location: ".$session->referrer."?edit=".$_POST['subEditLink']);
                }
        }
        /**
         * procDeleteLink - deletes posted link
         */
        function procDeleteLink()
        {
                global $session, $database;
               
                $database->deleteLink($_GET['deleteLink']);
                header("Location: ".$session->referrer);
        }
       
        /**
         * procAddFeedback - Attempts to add the feedback to the database.
         */
        function procAddFeedback()
        {
                global $session, $form;
                $retval = $session->addFeedback($_POST['subAddFeedback'], $_POST['name'], $_POST['email'], $_POST['subject'], $_POST['feedback'], $_POST['security_code']);

                if($retval == 0)// Feedback Successful
                {
                        $_SESSION['feedbacksuccess'] = true;
                        header("Location: ".$session->referrer);
                }
                else if($retval == 1)// Error found with form
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
                else if($retval == 2)// Feedback attempt failed
                {
                        $_SESSION['feedbacksuccess'] = false;
                        header("Location: ".$session->referrer);
                }
        }
        /**
         * procReviewedFeedback - sets feedback to viewed
         */
        function procReviewedFeedback()
        {
                global $session, $database;
               
                if($session->userlevel>USER_LEVEL)
                {
                        $database->query("UPDATE ".TBL_FEEDBACK." SET reviewed=1 WHERE id='".$_GET['reviewedFeedback']."'");
                }
                header("Location: ".$session->referrer);
        }
        /**
         * procDeleteFeedback - deletes posted feedback
         */
        function procDeleteFeedback()
        {
                global $session, $database;
               
                if($session->userlevel>USER_LEVEL)
                {
                        $database->deleteFeedback($_GET['deleteFeedback']);
                }
                header("Location: ".$session->referrer);
        }
        //admin
        function procMapAddressReset()
        {
                global $session, $database;
                $results = $database->query("SELECT * FROM ".TBL_USERS);

                while ($user = mysql_fetch_assoc($results)) {
                        if ($user["city"] && !$user["longitude"]) {
                                $google_data = file_get_contents("http://maps.google.com/maps/geo?q="
                                         . urlencode($user['zip'] . ", " . $user['city'] . ", " . $user['state'] . ", " . $user['country'])
                                         . "&output=json&oe=utf8&sensor=true_or_false&"
                                         . "key=" . GOOGLE_KEY);
               
                                $cut = substr($google_data, strpos($google_data, "\"coordinates\": [")+strlen("\"coordinates\": ["));
                                $latitude = substr($cut, 0, strpos($cut, ","));
                                $cut = substr($cut, strpos($cut, ",")+2);
                                $longitude = substr($cut, 0, strpos($cut, ","));
                                $coord[0] = $latitude;
                                $coord[1] = $longitude;
               
                                echo "<br>".$user["username"];
                                if (isset($coord[0]) && $coord[0] != 0) {
                                        $database->query("UPDATE ".TBL_USERS." SET longitude = "
                                                 . $coord[0] . ", latitude = " . $coord[1] .
                                                " WHERE username = '" . $user["username"] . "';");/*
                                        echo "UPDATE ".TBL_USERS." SET longitude = "
                                                 . $coord[0] . ", latitude = " . $coord[1] .
                                                " WHERE username = '" . $user["username"] . "';";*/
                                        echo " [X]";
                                        }
               
                                        $c ++;
                                        if ($c == 20)
                                                exit;
                                }
                        }
                header("Location: ".$session->referrer);
        }
        function procEditUserlevel()
        {
                global $session, $database;
                if($session->userlevel>=8)
                {
                        $database->updateUserField($_POST['subEditUserlevel'],'userlevel',$_POST['level']);
                }
                header("Location: ".$session->referrer);
        }
        function procCountryReset()
        {
                global $session, $database;
                $session->resetCountryCounts();
                $result = $database->query("SELECT username,country FROM ".TBL_USERS);
                $num_rows = mysql_num_rows($result);

                if($num_rows>0)
                {
                        for($i=0;$i<$num_rows;$i++)
                        {
                                $country = mysql_result($result,$i,"country");
                                $session->addCountryCount($country);
                        }
                }
                header("Location: ".$session->referrer);
        }

        function procUploadProfilePic()
        {
                global $session, $database, $form;
                if(($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/pjpeg" || $_FILES["file"]["type"] == "image/jpeg"))
                {
                        if($_FILES["file"]["size"] < 50000)
                        {
                                if($_FILES["file"]["error"] > 0)
                                {
                                  $form->setError("upload", "* File Error:".$_FILES["file"]["error"]);
                                }
                                else
                                {
                                        $con = ftp_connect(FTP_SERVER) or die("Could not connect");

                                        if(ftp_login($con, FTP_USER, FTP_PASS))
                                        {
                                                $randID = $session->generateRandID();
                                                $photopath = "includes/Profiles/".$randID.$_FILES["file"]["name"];
                                                if(file_exists(SITE_DIRECTORY.$photopath))
                                                {
                                                        $form->setError("upload", "* File already exists under: " . $_FILES["file"]["name"]);
                                                }
                                                else
                                                {
                                                        if(ftp_put($con,SITE_DIRECTORY.$photopath,$_FILES["file"]["tmp_name"],FTP_ASCII))
                                                        {
                                                                $database->addProfilePic($_POST['subUploadProfilePic'], $photopath);
                                                        }
                                                        else
                                                        {
                                                                $form->setError("uploading", "* Upload failed");
                                                        }
                                                }
                                        }
                                        else
                                        {
                                                $form->setError("uploading", "* Could not connect to server");
                                        }
                                        ftp_close($con);
                                }
                        }
                        else
                        {
                                $form->setError("upload", "* File too big 50KB max");
                        }
                }
                else
                {
                        $form->setError("upload", "* File not a GIF or JPEG file".$_FILES["file"]["size"]);
                }

                if($form->num_errors > 0)
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                else
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }

                header("Location: ".$session->referrer."?edit=".$_POST['subUploadProfilePic']);
        }
        function procUploadProjectPic()
        {
                global $session, $database, $form;
                if(($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/pjpeg" || $_FILES["file"]["type"] == "image/jpeg"))
                {
                        if($_FILES["file"]["size"] < 50000)
                        {
                                if($_FILES["file"]["error"] > 0)
                                {
                                  $form->setError("upload", "* File Error:".$_FILES["file"]["error"]);
                                }
                                else
                                {
                                        $con = ftp_connect(FTP_SERVER) or die("Could not connect");

                                        if(ftp_login($con, FTP_USER, FTP_PASS))
                                        {
                                                $randID = $session->generateRandID();
                                                $photopath = "includes/Projects/".$randID.$_FILES["file"]["name"];
                                                if(file_exists(SITE_DIRECTORY.$photopath))
                                                {
                                                        $form->setError("upload", "* File already exists under: " . $_FILES["file"]["name"]);
                                                }
                                                else
                                                {
                                                        if(ftp_put($con,SITE_DIRECTORY.$photopath,$_FILES["file"]["tmp_name"],FTP_ASCII))
                                                        {
                                                                $database->addProjectPic($_POST['subUploadProjectPic'], $photopath);
                                                        }
                                                        else
                                                        {
                                                                $form->setError("uploading", "* Upload failed");
                                                        }
                                                }
                                        }
                                        else
                                        {
                                                $form->setError("uploading", "* Could not connect to server");
                                        }
                                        ftp_close($con);
                                }
                        }
                        else
                        {
                                $form->setError("upload", "* File too big 50KB max");
                        }
                }
                else
                {
                        $form->setError("upload", "* File not a GIF or JPEG file");
                }

                if($form->num_errors > 0)
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editProjectFieldSuccess'] = false;
                }
                else
                {
                        $_SESSION['editProjectFieldSuccess'] = true;
                }

                header("Location: ".$session->referrer."?edit=".$_POST['subUploadProjectPic']);
        }

        /**
        * procLeaveProject() - Leaves the specified project
        */
        function procLeaveProject()
        {
                global $session, $form;
                if(strcmp($_GET['user'],GUEST)!=0)
                        $retval = $session->leaveProject($_GET['leaveProject'],$_GET['user']);
                else
                        $retval = false;

                if($retval)
                {
                        $_SESSION['leaveProjectSuccess'] = true;
                }
                else
                {
                        $_SESSION['leaveProjectSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_GET['leaveProject']);
        }
        /**
        * procJoinProject() - Joins the specified project
        */
        function procJoinProject()
        {
                global $session, $form;
                if(strcmp($_GET['user'],GUEST)!=0)
                        $retval = $session->joinProject($_GET['joinProject'],$_GET['user']);
                else
                        $retval = false;
               
                if($retval)
                {
                        $_SESSION['joinProjectSuccess'] = true;
                }
                else
                {
                        $_SESSION['joinProjectSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_GET['joinProject']);
        }
        /**
        * procApplication() - Edits content fields
        */
        function procApplication()
        {
                global $session, $form;
                $retval = $session->subApplication($_POST['to'],$_POST['from'],$_POST['subject'],$_POST['message'],$_POST['email'],$_POST['notifyType'],$_POST['subApplication']);

                if($retval)
                {
                        $_SESSION['subApplicationSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['subApplicationSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_POST['subApplication']);
        }
        /**
        * procDeleteProject() - Deletes a project from  the database
        */
        function procDeleteProject()
        {
                global $session;
                $retval = $session->deleteProject($_GET['deleteProject']);

                if($retval)
                {
                        $_SESSION['deleteProjectSuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteProjectSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procEditProjectPreferences() - Edits project preferences
        */
        function procEditProjectPreferences()
        {
                global $session, $form;
                $retval = $session->editProjectPreferences($_POST['subEditProjectPreferences'],$_POST['applyNotify'],$_POST['isPrivate'],isset($_POST['allowApply']));

                if($retval)
                {
                        $_SESSION['editProjectFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editProjectFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer."?edit=".$_POST['subEditProjectPreferences']);
        }
        /**
        * procEditProjectDetails() - Edits content fields
        */
        function procEditProjectDetails()
        {
                global $session, $form;
                $retval = $session->editProjectDetails($_POST['subEditProjectDetails'],$_POST['link'],$_POST['moneyNeeded'],$_POST['email']);

                if($retval)
                {
                        $_SESSION['editProjectFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editProjectFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer."?edit=".$_POST['subEditProjectDetails']);
        }
        /**
        * procEditProjectField() - Edits content fields
        */
        function procEditProjectField()
        {
                global $session, $form;
                $retval = $session->editProjectField($_POST['projectID'],$_POST['subEditProjectField'],$_POST['content']);

                if($retval)
                {
                        $_SESSION['editProjectFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editProjectFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer."?edit=".$_POST['projectID']);
        }
        /**
        * procEditMemberlevel() - Edit member level
        */
        function procEditMemberlevel()
        {
                global $session;
                $retval = $session->editMemberLevel($_POST['projectID'],$_POST['subEditMemberlevel'],$_POST['level']);

                if($retval)
                {
                        $_SESSION['editProjectMember'] = true;
                }
                else
                {
                        $_SESSION['editProjectMember'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_POST['projectID']);
        }
        /**
        * procEditMemberlevel() - Deletes project member
        */
        function procDeleteProjectMember()
        {
                global $session;
                $retval = $session->deleteProjectMember($_GET['projectID'],$_GET['deleteMember']);

                if($retval)
                {
                        $_SESSION['deleteProjectMember'] = true;
                }
                else
                {
                        $_SESSION['deleteProjectMember'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_GET['projectID']);
        }

        /**
        * procAddNeededResource() - Adds resource to database
        */
        function procAddNeededResource()
        {
                global $session, $form;
                $retval = $session->addNeededResource($_POST['subAddNeededResource'],$_POST['type'],$_POST['category'],$_POST['title'],$_POST['description']);

                if($retval)
                {
                        $_SESSION['addResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['addResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_POST['subAddNeededResource']);
        }
        /**
        * procEditNeededResource() - Edits resource in the database
        */
        function procEditNeededResource()
        {
                global $session, $form;
                $retval = $session->editNeededResource($_POST['subEditNeededResource'],$_POST['type'],$_POST['category'],$_POST['title'],$_POST['description']);

                if($retval)
                {
                        $_SESSION['editResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_POST['projectID']."&edit=".$_POST['subEditNeededResource']);
        }
        /**
        * procDeleteNeededResource() - Deletes a resource from database
        */
        function procDeleteNeededResource()
        {
                global $session, $database;
                $retval = $database->deleteNeededResource($_GET['deleteNeededResource'],$_GET['id']);

                if($retval)
                {
                        $_SESSION['deleteResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer."?id=".$_GET['id']);
        }

        /**
        * procAddProject() - Validates fields and stores into database
        * returns true if successful.
        */
        function procAddProject()
        {
                global $session, $form;
                $retval = $session->addProject($_POST['subAddProject'],$_POST['name'],$_POST['description'],$_POST['isPrivate']);

                if($retval)
                {
                        $_SESSION['addProjectSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['addProjectSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }

        /**
        * procSearch() - Validates search terms.
        */
        function procSearch()
        {
                global $session, $form;
                $retval = $session->checkSearch($_POST['text']);

                if($retval)
                {
                        $_SESSION['searchSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['searchSuccess'] = false;
                }
                $jumpTo = "Location: ".$session->referrer;
                $prev = false;
                if(isset($_POST['type']) && $_POST['type']!="")
                {
                        $jumpTo .= "?type=".$_POST['type'];
                        $prev = true;
                }
                if(isset($_POST['category']) && $_POST['category']!="")
                {
                        if($prev)
                                $jumpTo .= "&";
                        else
                                $jumpTo .= "?";
                        $jumpTo .= "category=".$_POST['category'];
                        $prev = true;
                }
                if(isset($_POST['text']) && $_POST['text']!="")
                {
                        if($prev)
                                $jumpTo .= "&";
                        else
                                $jumpTo .= "?";
                        $jumpTo .= "text=".$_POST['text'];
                        $prev = true;
                }
                if(isset($_POST['country']) && $_POST['country']!="")
                {
                        if($prev)
                                $jumpTo .= "&";
                        else
                                $jumpTo .= "?";
                        $jumpTo .= "country=".$_POST['country'];
                        $prev = true;
                }
                header($jumpTo);
        }

        /**
        * procDeleteCategory() - Deletes the category from the database
        */
        function procDeleteCategory()
        {
                global $session, $database;
                $retval = $database->deleteCategory($_GET['deleteCategory']);

                if($retval)
                {
                        $_SESSION['deleteCategorySuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteCategorySuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procAddCategory() - Adds category to database
        */
        function procAddCategory()
        {
                global $session, $form;
                $retval = $session->addCategory($_POST['type'],$_POST['category']);

                if($retval)
                {
                        $_SESSION['addCategorySuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['addCategorySuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }

        /**
        * procDeleteUser() - Deletes the user from database
        */
        function procDeleteUser()
        {
                global $session, $database;
                $retval = $database->deleteUser($_GET['deleteUser']);

                if($retval)
                {
                        $_SESSION['deleteUserSuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteUserSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }

        /**
        * procDeleteMessage() - Deletes the message from database
        */
        function procDeleteMessage()
        {
                global $session, $database;
                $retval = $database->deleteMessage($_GET['deleteMessage'],$session->username);

                if($retval)
                {
                        $_SESSION['deleteMessageSuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteMessageSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procSendMessage() - Stores a message from a user for another user.
        */
        function procSendMessage()
        {
                global $session, $form;
                $retval = $session->sendMessage($_POST['subSendMessage'],$_POST['subject'],$_POST['message']);

                if($retval)
                {
                        $_SESSION['sendMessageSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['sendMessageSuccess'] = false;
                }
                header("Location: ".$session->referrer."?send=".$_GET['send']);
        }

        /**
        * procDeleteResource() - Deletes a resource from database
        */
        function procDeleteResource()
        {
                global $session, $database;
                $retval = $database->deleteResource($_GET['deleteResource'],$session->username);

                if($retval)
                {
                        $_SESSION['deleteResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['deleteResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procAddResource() - Adds resource to database
        */
        function procAddResource()
        {
                global $session, $form;
                //splits into latitude and longitude $chop[0] $chop[1]
                $chop = $_POST['LatLong'];
                $chop = str_replace("(", "", $chop);
                $chop = str_replace(")", "", $chop);
                $chop = str_replace(" ", "", $chop);
                $chop = explode(",",$chop);
                $retval = $session->addResource($_POST['type'],$_POST['category'],$_POST['title'],$_POST['description'],$chop[0],$chop[1]);

                if($retval)
                {
                        $_SESSION['addResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['addResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procEditResource() - Edits resource in the database
        */
        function procEditResource()
        {
                global $session, $form;
                //splits into latitude and longitude $chop[0] $chop[1]
                $chop = $_POST['LatLong'];
                $chop = str_replace("(", "", $chop);
                $chop = str_replace(")", "", $chop);
                $chop = str_replace(" ", "", $chop);
                $chop = explode(",",$chop);
                $retval = $session->editResource($_POST['subEditResource'],$_POST['type'],$_POST['category'],$_POST['title'],$_POST['description'],$chop[0],$chop[1]);

                if($retval)
                {
                        $_SESSION['editResourceSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editResourceSuccess'] = false;
                }
                header("Location: ".$session->referrer."?edit=".$_POST['subEditResource']);
        }

        /**
        * procDeleteComment() - Deletes comment from database
        */
        function procDeleteComment()
        {
                global $session, $database;
                if($session->userlevel>1)
                {
                        $database->deleteComment($_GET['deleteComment']);
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procAddComment() - Adds comment to specified attachment
        */
        function procAddComment()
        {
                global $session, $form;
                $retval = $session->addComment($_POST['subAddComment'],$_POST['comment'],$session->username);

                if($retval)
                {
                        $_SESSION['addCommentSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['addCommentSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }

        /**
        * procPreferences() - Edits user's preferences
        */
        function procEditPreferences()
        {
                global $session, $form;
                $retval = $session->editPreferences(isset($_POST['messageNotify']),isset($_POST['displayEmail']));

                if($retval)
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
         * procLocation() - Edits user's latlong location
         */
        function procEditLocation()
        {
                global $session, $form;
                $chop = $_POST['LatLong'];
                $chop = str_replace("(", "", $chop);
                $chop = str_replace(")", "", $chop);
                $chop = str_replace(" ", "", $chop);
                $chop = explode(",",$chop);
                $retval = $session->editLocation($chop[0], $chop[1]);

                if($retval)
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procEditContactInfo() - Edits user's optional contact info
        */
        function procEditContactInfo()
        {
                global $session, $form;
                $retval = $session->editContactInfo($_POST['email'],$_POST['aim'],$_POST['msn'],$_POST['yahoo'],$_POST['icq'],$_POST['skype'],$_POST['jabber']);

                if($retval)
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procEditUserInfo() - Edits user's optional info
        */
        function procEditUserInfo()
        {
                global $session, $form;
                $retval = $session->editUserInfo($_POST['fname'],$_POST['lname'],$_POST['gender'],$_POST['country'],$_POST['state'],$_POST['city'],$_POST['zip']);

                // this is the procidure to convert zip code and stuff to location
                $google_data = file_get_contents("http://maps.google.com/maps/geo?q="
                . urlencode( $_POST['zip'] . ", " . $_POST['city'] . ", " . $_POST['state'] . ", " . $_POST['country'] )
                . "&output=json&oe=utf8&sensor=true_or_false&"
                . "key=" . GOOGLE_KEY);

                // since JSON_parse is not used, i need to parse myself
                $cut = substr($google_data, strpos($google_data, "\"coordinates\": [")+strlen("\"coordinates\": ["));
                $latitude = substr($cut, 0, strpos($cut, ","));
                $cut = substr($cut, strpos($cut, ",")+2);
                $longitude = substr($cut, 0, strpos($cut, ","));

                if ($retval && isset($latitude) && $latitude != 0) {
                        $retval = $session->editLocation($longitude,$latitude);
                }


                if($retval)
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }
        /**
        * procEditUserField() - Edits content fields
        */
        function procEditUserField()
        {
                global $session, $form;
                $retval = $session->editUserField($_POST['subEditUserField'],$_POST['content']);

                if($retval)
                {
                        $_SESSION['editUserFieldSuccess'] = true;
                }
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        $_SESSION['editUserFieldSuccess'] = false;
                }
                header("Location: ".$session->referrer);
        }

        /**
        * procPostNews() - Posts news
        */
        function procPostNews()
        {
                global $session, $database;

                $time = time();
                $title = $_POST['title'];
                $content = $_POST['content'];
                $user = $session->username;
                $randID = $session->generateRandID();

                $database->query("INSERT INTO ".TBL_NEWS." VALUES ('$randID', '$title', '$content', '$time', '$user')");

                $_SESSION['postnewssuccess'] = true;
                header("Location: ".$session->referrer);
        }

        /**
        * procEditNews() - Edits news
        */
        function procEditNews()
        {
                global $session, $database;

                $id = $_POST['subEditNews'];
                $title = $_POST['title'];
                $content = $_POST['content'];

                $database->query("UPDATE ".TBL_NEWS." SET title = '$title', content = '$content' WHERE id = '$id'");

                $_SESSION['editnewssuccess'] = true;
                header("Location: ".$session->referrer);
        }

        /**
        * procLogin - Processes the user submitted login form, if errors
        * are found, the user is redirected to correct the information,
        * if not, the user is effectively logged in to the system.
        */
        function procLogin()
        {
                global $session, $form;
                /* Login attempt */
                $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));

                /* Login successful */
                if($retval)
                {
                        header("Location: ".$session->referrer);
                }
                /* Login failed */
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
        }

        /**
        * procLogout - Simply attempts to log the user out of the system
        * given that there is no logout form to process.
        */
        function procLogout()
        {
                global $session;
                $retval = $session->logout();
                header("Location: ".SITE_URL."index.php");
        }

        /**
        * procRegister - Processes the user submitted registration form,
        * if errors are found, the user is redirected to correct the
        * information, if not, the user is effectively registered with
        * the system and an email is (optionally) sent to the newly
        * created user.
        */
        function procRegister()
        {
                global $session, $form;
                /* Convert username to all lowercase (by option) */
                if(ALL_LOWERCASE)
                {
                        $_POST['user'] = strtolower($_POST['user']);
                }
                /* Registration attempt */
                $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['country'], $_POST['gender'], $_POST['security_code']);

                /* Registration Successful */
                if($retval == 0)
                {
                        $_SESSION['reguname'] = $_POST['user'];
                        $_SESSION['regsuccess'] = true;
                        header("Location: ".$session->referrer);
                }
                /* Error found with form */
                else if($retval == 1)
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
                /* Registration attempt failed */
                else if($retval == 2)
                {
                        $_SESSION['reguname'] = $_POST['user'];
                        $_SESSION['regsuccess'] = false;
                        header("Location: ".$session->referrer);
                }
        }

        /**
        * procChangePass - Changes user password
        */
        function procChangePass()
        {
                global $session, $form;
                /* Registration attempt */
                $retval = $session->changePass($session->username, $_POST['oldpass'], $_POST['newpass'], $_POST['confirmnewpass']);

                /* Registration Successful */
                if($retval)
                {
                        $_SESSION['changepass'] = true;
                        header("Location: ".$session->referrer);
                }
                /* Error found with form */
                else if(!$retval)
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
        }
       
        /**
        * procForgotPass - Validates the given username then if
        * everything is fine, a new password is generated and
        * emailed to the address the user gave on sign up.
        */
        function procForgotPass()
        {
                global $database, $session, $mailer, $form;
                /* Username error checking */
                $subuser = $_POST['user'];
                $field = "user";  //Use field name for username
                if(!$subuser || strlen($subuser = trim($subuser)) == 0)
                {
                        $form->setError($field, "* Username not entered");
                }
                else
                {
                        /* Make sure username is in database */
                        $subuser = stripslashes($subuser);
                        if(strlen($subuser) < 5 || strlen($subuser) > 30 || !eregi("^([0-9a-z])+$", $subuser) || (!$database->usernameTaken($subuser)))
                        {
                                $form->setError($field, "* Username does not exist");
                        }
                }

                /* Errors exist, have user correct them */
                if($form->num_errors > 0)
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                }
                /* Generate new password and email it to user */
                else
                {
                        /* Generate new password */
                        $newpass = $session->generateRandStr(8);
                       
                        $result = $database->query("SELECT email FROM ".TBL_USERS." WHERE username='$subuser'");
                        $arrayUser = mysql_fetch_array($result);

                        /* Attempt to send the email with new password */
                        if($mailer->sendNewPass($subuser,$arrayUser['email'],$newpass))
                        {
                                /* Email sent, update database */
                                $database->updateUserField($subuser, "password", md5($newpass));
                                $_SESSION['forgotpass'] = true;
                        }
                        /* Email failure, do not change password */
                        else
                        {
                                $_SESSION['forgotpass'] = false;
                        }
                }

                header("Location: ".$session->referrer);
        }

        /**
        * procEditAccount - Attempts to edit the user's account
        * information, including the password, which must be verified
        * before a change is made.
        */
        function procEditAccount()
        {
                global $session, $form;
                /* Account edit attempt */
                $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);

                /* Account edit successful */
                if($retval)
                {
                        $_SESSION['useredit'] = true;
                        header("Location: ".$session->referrer);
                }
                /* Error found with form */
                else
                {
                        $_SESSION['value_array'] = $_POST;
                        $_SESSION['error_array'] = $form->getErrorArray();
                        header("Location: ".$session->referrer);
                }
        }
};

/* Initialize process */
$process = new Process;

?>

